package org.cboard.query;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.cboard.util.TreeList;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("tree")
public class TreeDyParamHandler implements DyParamHandler {

    public String getValue(QueryParameter pp, String value) {
        if (value == null || "".equals(value)) {
            JSONObject json = JSONObject.parseObject(pp.getDefaultValue());
            value = json.getString("key");
        }
        return value;
    }

    public List<TreeList> getSqlParamValue(String[][] data) {
        if(data==null){
            return null;
        }
        int col=data[0].length;
        if(col!=3){//查询不匹配
            throw new RuntimeException("tree类型，参数必须为3列");
        }
        List<TreeList> list = Lists.newArrayList();
        int len=data.length;
        for (int i = 1; i < len; i++) {//i=1,因为第一行为表头
            TreeList tt = new TreeList();
            tt.setId(Integer.parseInt(data[i][0]));
            if (Integer.parseInt(data[i][1]) == 0) {
                tt.setParent("#");
            } else {
                tt.setParent(data[i][1]);
            }
            tt.setText(data[i][2]);
            list.add(tt);
        }
        return list;
    }


}
